home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 05.zip / BS1 part 5 / SASC_6.0_Disk_7.adf / Source_And_Examples / source / _main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-30  |  5.2 KB  |  209 lines

  1. /***
  2. *
  3. *          Copyright © 1992 SAS Institute, Inc.
  4. *
  5. * name             __main - process command line, open files, and call main()
  6. *
  7. * synopsis         __main(line);
  8. *                  char *line;     ptr to command line that caused execution
  9. *
  10. * description      This function performs the standard pre-processing for
  11. *                  the main module of a C program.  It accepts a command
  12. *                  line of the form
  13. *
  14. *                       pgmname arg1 arg2 ...
  15. *
  16. *                  and builds a list of pointers to each argument.  The first
  17. *                  pointer is to the program name.  For some environments, the
  18. *                  standard I/O files are also opened, using file names that
  19. *                  were set up by the OS interface module XCMAIN.
  20. *
  21. ***/
  22.  
  23. #include <stdio.h>
  24. #include <fcntl.h>
  25. #include <ios1.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <time.h>
  29. #include <workbench/startup.h>
  30. #include <libraries/dos.h>
  31. #include <libraries/dosextens.h>
  32. #include <proto/dos.h>
  33. #include <proto/exec.h>
  34. #include <exec/execbase.h>
  35.  
  36. extern struct ExecBase *SysBase;
  37.  
  38. #define MAXARG 32
  39. #define QUOTE       '"'
  40. #define MAXWINDOW   40
  41. #define ESCAPE '*'
  42. #define ESC '\027'
  43. #define NL '\n'
  44.  
  45. #define isspace(c)      ((c == ' ')||(c == '\t') || (c == '\n'))
  46.  
  47.  
  48. #ifndef TINY
  49. extern int __fmode;
  50. #endif
  51.  
  52. extern char __stdiowin[];
  53. extern char __stdiov37[];
  54.  
  55. extern struct WBStartup *_WBenchMsg;
  56. int main(int, void *);
  57.  
  58. static int argc;                            /* arg count */
  59. static char **targv, *argv[MAXARG+1];       /* arg pointers */
  60.  
  61.  
  62.  
  63.  
  64. void __stdargs __main(line)
  65.     char *line;
  66. {
  67.     char **pargv;
  68.     char *argbuf;
  69.     int ret;
  70.  
  71.  
  72. #ifndef TINY
  73.     int x;
  74.     struct Process *process;
  75.     struct FileHandle *handle;
  76.     struct UFB  ufbs[3];
  77.     static char window[MAXWINDOW+18];
  78. #endif
  79.  
  80.  
  81. /***
  82. *     Build argument pointer list
  83. ***/
  84.    while (argc < MAXARG)
  85.    {
  86.         while (isspace(*line))  line++;
  87.         if (*line == '\0')      break;
  88.         pargv = &argv[argc++];
  89.         if (*line == QUOTE)
  90.         {
  91.             argbuf = *pargv = ++line;  /* ptr inside quoted string */
  92.             while (*line != QUOTE && *line != 0)
  93.             {
  94.                if (*line == ESCAPE)
  95.                {
  96.                   line++;
  97.                   switch (*line)
  98.                   {
  99.                      case '\0':
  100.                         *argbuf = 0;
  101.                         goto linedone;
  102.                      case 'E':
  103.                         *argbuf++ = ESC;
  104.                         break;
  105.                      case 'N':
  106.                         *argbuf++ = NL;
  107.                         break;
  108.                      default:
  109.                         *argbuf++ = *line;
  110.                   }
  111.                   line++;
  112.                }
  113.                else
  114.                {
  115.                  *argbuf++ = *line++;
  116.                }
  117.             }
  118.             line++;
  119.             *argbuf++ = '\0'; /* terminate arg */
  120.         }
  121.         else            /* non-quoted arg */
  122.         {       
  123.             *pargv = line;
  124.             while ((*line != '\0') && (!isspace(*line))) line++;
  125.             if (*line == '\0')  break;
  126.             else                *line++ = '\0';  /* terminate arg */
  127.         }
  128.    }  /* while */
  129.  
  130. linedone:
  131.  
  132.     targv = (argc == 0) ? (char **) _WBenchMsg : (char **) &argv[0];
  133.  
  134.  
  135. /***
  136. *     Open standard files
  137. ***/
  138.  
  139. #ifndef TINY
  140.     ufbs[0].ufbnxt = &ufbs[1];
  141.     ufbs[1].ufbnxt = &ufbs[2];
  142.     ufbs[2].ufbnxt = NULL;
  143.     __ufbs = &ufbs[0];
  144.     ufbs[0].ufbfn = NULL;
  145.     ufbs[1].ufbfn = NULL;
  146.     ufbs[2].ufbfn = NULL;
  147.     ufbs[0].ufbflg = UFB_RA | O_RAW | UFB_NC;
  148.     ufbs[1].ufbflg = UFB_WA | O_RAW | UFB_NC;
  149.     ufbs[2].ufbflg = UFB_WA | O_RAW | UFB_NC | UFB_CLO;
  150.  
  151.     if (argc == 0) 
  152.     {             /* running under workbench      */
  153.         if (_WBenchMsg->sm_ToolWindow)
  154.             ufbs[0].ufbfh = Open(_WBenchMsg->sm_ToolWindow, MODE_NEWFILE);
  155.         else
  156.         {
  157.            strcpy(window, __stdiowin);
  158.            strncat(window, _WBenchMsg->sm_ArgList->wa_Name, MAXWINDOW);
  159.         
  160.            if (SysBase->LibNode.lib_Version >= 36)
  161.               strncat(window, __stdiov37, MAXWINDOW+18-strlen(window)-1); 
  162.             
  163.            ufbs[0].ufbfh = Open(window, MODE_NEWFILE);
  164.         }
  165.         
  166.         ufbs[0].ufbflg |= UFB_CLO;
  167.         handle = (struct FileHandle *) (ufbs[0].ufbfh << 2);
  168.         process = (struct Process *) FindTask(0);
  169.         process->pr_ConsoleTask = (APTR) handle->fh_Type;
  170.         if ((ufbs[1].ufbfh = Open("*", MODE_OLDFILE)) == NULL)
  171.              ufbs[1].ufbfh = Open("NIL:", MODE_OLDFILE);
  172.         ufbs[2].ufbfh = ufbs[1].ufbfh;
  173.     } 
  174.     else 
  175.     {                     /* running under CLI            */
  176.         ufbs[0].ufbfh = Input();
  177.         ufbs[1].ufbfh = Output();
  178.         if ((ufbs[2].ufbfh = Open("*", MODE_OLDFILE)) == NULL)
  179.              ufbs[2].ufbfh = Open("NIL:", MODE_OLDFILE);
  180.     }
  181.  
  182.  
  183.     __nufbs += 3;
  184.  
  185.  
  186.     x = (__fmode) ? 0 : _IOXLAT;
  187.     stdin->_file = 0;
  188.     stdin->_flag = _IOREAD | x;
  189.     stdout->_file = 1;
  190.     stdout->_flag = _IOWRT | _IOLBF | x;
  191.     stderr->_file = 2;
  192.     stderr->_flag = _IOWRT | _IOLBF | x;
  193.  
  194. #endif
  195.  
  196.  
  197. /***
  198. *     Call user's main program
  199. ***/
  200.  
  201.     ret = main(argc, targv);                /* call main function */
  202.  
  203. #ifndef TINY
  204.     exit(ret);
  205. #else
  206.     __exit(ret);
  207. #endif
  208. }
  209.